package com.leonzhangxf.ioc.injection.method;

import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Component;

/**
 * A command manager for process message.
 *
 * @author leonzhangxf
 */
@Component
public abstract class CommandManager {

    /**
     * To process the message
     *
     * @param msg message to be processed.
     * @return the result of the procession.
     */
    public Object process(String msg) {
        Command command = createCommand();
        System.out.println(String.format("The command's memory address is %s.", command));
        command.setMessage(msg);
        return command.exec();
    }

    /**
     * Create a command for use.
     * <p>
     * 使用 {@link Lookup} 注解来让spring帮助在singleton bean每次调用时生成并注入prototype bean。
     *
     * @return the wanted command.
     */
    @Lookup
    protected abstract Command createCommand();
}
